using System;
using System.Collections.Generic;
using System.Drawing;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Faces.Base;

namespace FireStarterModeller.UI
{
    public class PropertyTypeMapper : ExtendedComboBoxMapper
    {
        private IDMSolution _dmSolution;
        private IBOGridAndEditorControl EditorControl { get; set; }

        public PropertyTypeMapper(IExtendedComboBox ctl, string propName, bool isReadOnly, IControlFactory controlFactory)
            : base(ctl, propName, isReadOnly, controlFactory)
        {
        }

        protected override void ReloadLookupValues()
        {
            LoadPropertyTypeCombo();
        }

        [CoverageExclude]
        public override void ShowPopupForm()
        {
            if (_dmSolution == null)
            {
                const string message = "Please set the DMSolution first";
                throw new HabaneroDeveloperException(message, message);
            }
            PopupForm = ControlFactory.CreateForm();
            PopupForm.Height = 600;
            PopupForm.Width = 800;
            PopupForm.MinimumSize = new Size(800, 600);
            //PopupForm.StartPosition = FormStartPosition.CenterScreen;
            IBOGridAndEditorControl editorControl = ControlFactory.CreateGridAndBOEditorControl
                (Habanero.BO.ClassDefinition.ClassDef.Get<DMPropertyType>());
            editorControl.GridControl.Width = 370;
            
            EditorControl = editorControl;
//            IButton control = EditorControl.ButtonGroupControl["Delete"];
//            control.Visible = false;
//            EditorControl.ButtonGroupControl.AddButton("CustomDelete", "Delete", HandleDeleteButtonClick)
            EditorControl.Dock = DockStyle.Fill;
            PopupForm.Controls.Add(EditorControl);
            EditorControl.BusinessObjectCollection = _dmSolution.PropertyTypes;
        }

        protected override void HandlePopUpFormClosedEvent(object sender, EventArgs e)
        {
            var iboGridAndEditorControl = GetIBOGridAndEditorControl();
            var currentBusinessObject = iboGridAndEditorControl.CurrentBusinessObject;
            if (currentBusinessObject != null)
            {
                if (currentBusinessObject.IsValid())
                {
                    currentBusinessObject.Save();
                }else
                {
                    currentBusinessObject.MarkForDelete();
                }
            }
            ReloadLookupValues();
            if (currentBusinessObject != null)
            {
                ExtendedComboBox.ComboBox.SelectedValue = currentBusinessObject.ID.GetAsValue().ToString();
            }
        }
        public IDMSolution DMSolution
        {
            get { return _dmSolution; }
            set { _dmSolution = value; }
        }


        public void LoadPropertyTypeCombo()
        {
            if (_dmSolution == null) return;
            var propertyTypes = _dmSolution.PropertyTypes;
            var displayValueDictionary =
                BusinessObjectLookupList.CreateDisplayValueDictionary(propertyTypes, true, Convert.ToString);
            this.LookupList = displayValueDictionary;
        }
    }
}